//搜索框
const inp = document.querySelector('.main>input')
const btn = document.querySelector('.main>button')
const li =document.querySelectorAll('.main>ul>li')
const ul = document.querySelector('.main>ul')
inp.oninput = function(){
    const worde = this.value.trim()
    const script = document.createElement('script')
    script.src = 'https://category.vip.com/ajax/getSuggest.php?callback=fn&keyword= '+ worde +' &_=1635428619443'
    document.body.appendChild(script)
    script.remove()
    // console.log(script);
}
function fn (res){
    console.log(res);
    if(res.data.length === 0)return ul.style.display = 'none'
    let str =''
    res.data.forEach(item => {
        str += `<li>${item.word}</li>`
    });
    ul.innerHTML =str
    ul.style.display = 'block'
}
$('.w12').on('blur', function () {
    $('.seach>li').click(function () {
        $('.w12').val($(this).text())
        $('.seach').html('')
        $('.seach').css('displat','none')
    })
})
//优惠券跳转
$('.quan>ul>li').click(function(){
    window.location.href ='https://lingquan.gome.com.cn/?intcmp=sy-1000079896-1'
})
//政务信息下拉
$('.zw').hover(function(){
    $('#texun').stop().slideDown(1500).hover(function(){
        $('#texun').show()
    },function(){
        $('#texun').stop().slideUp(1000)
    })
}
)
//回到顶部
$(window).scroll(function(){
    const scrollTop = document.documentElement.scrollTop || document.body.scrollTop
    if(scrollTop >= 700){
        $('.huidao').stop().fadeIn()
    }else{
        $('.huidao').stop().fadeOut()
    }
})
$('.huidao').click(function(){
    $('html,body').animate({scrollTop:0})
})

//下拉列表
tt()
$('.cd-hm>li').on('mouseout',function(){
    $('.cd-hm>li').removeClass('xianshi')
    $('.cd').stop().slideUp(500)
})
function tt(){
    $('.qbsp').hover(function(){
        $('.cd').stop().slideDown(500)   
    },function(){
    })
    $('.cd>li').mouseover(function (){
        $(this)
        .parent()
        .next()
        .children()
        .eq($(this).index())
        .addClass('xianshi')
        .siblings()
        .removeClass('xianshi')
    })
}
//登录响应
const nickname = document.querySelector('.nickname')
//登录验证
testLogin()
function testLogin(){
    const id = window.localStorage.getItem('id')
    const token =window.localStorage.getItem('token')
    console.log(id);
    console.log(token);
    ajax({
        url:'http://localhost:8888/users/info',
        method:'get',
        dataType:'json',
        data:'id='+id,
        headers:{ 'authorization': 'token'},
        success(res){
            if(res.code === 1 ){
                nickname.innerHTML = res.info.nickname
            }
        }
    })
}


const tuichu = document.querySelector('.tuichu')
tuichu.onclick = function(){
      layer.confirm('您确定退出？', {
        btn: ['确定','取消'] //按钮
      }, function(){
        window.localStorage.removeItem('token')
        window.localStorage.removeItem('id')
        ajax({
            url: 'http://localhost:8888/users/logout',
            success(){
                window.location.reload()
            }
        })
      }, function(){
      });
    
   
}